package com.party.mobile.biz.analyze;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.DataParam;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.model.main.MainViewAnalyze;
import com.party.core.model.member.*;
import com.party.core.model.order.OrderForm;
import com.party.core.service.copartner.ITargetCopartnerService;
import com.party.core.service.main.IMainViewAnalyzeService;
import com.party.core.service.member.*;
import com.party.core.service.order.IOrderFormService;
import com.party.core.service.partner.IMemberPartnerPrivilegeService;
import com.party.mobile.web.dto.analyze.output.AnalyzeListOutput;
import com.party.mobile.web.dto.analyze.output.CopartnerAnalyzeListOutput;
import com.party.mobile.web.dto.analyze.output.MerchantAnalyzeListOutput;
import com.party.mobile.web.dto.analyze.output.MerchantAnalyzeOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.*;

/**
 * 数据分析业务接口
 * Created by wei.li
 *
 * @date 2017/9/7 0007
 * @time 10:49
 */
@Service
public class AnalyzeBizService {

    @Autowired
    private IMerchantAnalyzeService merchantAnalyzeService;

    @Autowired
    private ICopartnerAnalyzeService copartnerAnalyzeService;

    @Autowired
    private ITargetCopartnerService targetCopartnerService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IMemberMerchantDetailService memberMerchantDetailService;

    @Autowired
    private IMemberMerchantCountService memberMerchantCountService;

    @Autowired
    private IMemberCopartnerCountService memberCopartnerCountService;

    @Autowired
    private IMemberCopartnerDetailService memberCopartnerDetailService;

    @Autowired
    private IMemberPartnerPrivilegeService memberPartnerPrivilegeService;

    @Autowired
    private IMemberAdministratorService memberAdministratorService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMainViewAnalyzeService mainViewAnalyzeService;

    private static Integer END_DATE_INDEX = 6;

    /**
     * 统计列表
     * @param memberId 会员编号
     * @return 列表数据
     */
    public HashMap<String, AnalyzeListOutput> list(String memberId){
        HashMap<String, AnalyzeListOutput> map = Maps.newHashMap();

        String now = DateUtils.todayDate();
        MemberMerchantDetailQuery memberMerchantDetailQuery = new MemberMerchantDetailQuery();
        memberMerchantDetailQuery.setMemberId(memberId);
        memberMerchantDetailQuery.setCreateStart(now);
        memberMerchantDetailQuery.setCreateEnd(now);
        boolean isQueryChild = memberGroupService.isQueryChild(memberId);
        memberMerchantDetailQuery.setIsQueryChild(isQueryChild ? 1 : 0);

        //今天
        MemberMerchantDetail today = memberMerchantDetailService.sum(memberMerchantDetailQuery);
        AnalyzeListOutput todayData
                = new AnalyzeListOutput(today.getApplyNum(), today.getOrderNum(),
                today.getMoney(), today.getViewNum().longValue(), today.getMemberNum(), today.getSuccessNum());
        map.put("today", todayData);


        //昨天
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        memberMerchantDetailQuery.setCreateStart(day);
        memberMerchantDetailQuery.setCreateEnd(day);
        MemberMerchantDetail yesterday = memberMerchantDetailService.sum(memberMerchantDetailQuery);
        AnalyzeListOutput yesterdayData
                = new AnalyzeListOutput(yesterday.getApplyNum(), yesterday.getOrderNum(),
                yesterday.getMoney(), yesterday.getViewNum().longValue(), yesterday.getMemberNum(), yesterday.getSuccessNum());
        map.put("yesterday", yesterdayData);

        //本周
        memberMerchantDetailQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        memberMerchantDetailQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MemberMerchantDetail week = memberMerchantDetailService.sum(memberMerchantDetailQuery);
        AnalyzeListOutput weekData
                = new AnalyzeListOutput(week.getApplyNum(), week.getOrderNum(),
                week.getMoney(), week.getViewNum().longValue(), week.getMemberNum(), week.getSuccessNum());
        map.put("week", weekData);

        //本月
        memberMerchantDetailQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        memberMerchantDetailQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MemberMerchantDetail month = memberMerchantDetailService.sum(memberMerchantDetailQuery);
        AnalyzeListOutput monthData
                = new AnalyzeListOutput(month.getApplyNum(), month.getOrderNum(),
                month.getMoney(), month.getViewNum().longValue(), month.getMemberNum(), month.getSuccessNum());
        map.put("month", monthData);

        //历史
        MemberMerchantCount history = memberMerchantCountService.findByMemberIdSum(memberId, isQueryChild);
        AnalyzeListOutput historyData
                = new AnalyzeListOutput(history.getApplyNum(), history.getOrderNum(),
                history.getMoney(), history.getViewNum(), history.getMemberNum(), history.getSuccessNum());
        map.put("history", historyData);
        return map;
    }

    /**
     * 获取时间列表
     * @return 时间列表
     */
    public List<Date> dateList(){
        Date now = new Date();
        Date startDate = DateUtils.addDay(now, -END_DATE_INDEX);
        List<Date> dateList =  DateUtils.getDateDifference(startDate, now);
        return dateList;
    }


    /**
     * 获取字符串时间列表
     * @return 列表
     */
    public List<String> dateStringList(){
        List<Date> list = dateList();
        List<String> dateList = Lists.newArrayList();
        for (Date date : list){
            dateList.add(DateUtils.formatDate(date, "MM/dd"));
        }
        return dateList;
    }

    /**
     * 根据统计单位获取时间列表
     * @param unit 单位（0:7天 1:6周 2:6个月）
     * @return 时间列表
     */
    public List<String> dateStringList(Integer unit){
        List<String> dateList = Lists.newArrayList();
        //按周计算
        if (unit.equals(1)){
            dateList = Lists.newArrayList("第一周", "第二周", "第三周", "第四周", "第五周(上周)", "第六周(本周)");
        }
        //按月计算
        else if (unit.equals(2)){
            for (int i= 5; i>=0; i--){
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        } else if (unit.equals(3)){
            for (int i= 11; i>=0; i--){
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        }else {
            List<Date> list = dateList();
            for (Date date : list){
                dateList.add(DateUtils.formatDate(date, "MM/dd"));
            }
        }
        return dateList;
    }

    /**
     * 统计数列表
     * @param memberId 会员编号
     * @return 数据列表
     */
    public List<MemberMerchantDetail> analyzeList(String memberId){
        String now = DateUtils.todayDate();
        String startDay = DateUtils.formatDate(DateUtils.addDay(now, -7));
        MemberMerchantDetailQuery memberMerchantDetailQuery = new MemberMerchantDetailQuery();
        memberMerchantDetailQuery.setMemberId(memberId);
        memberMerchantDetailQuery.setCreateStart(startDay);
        memberMerchantDetailQuery.setCreateEnd(now);
        boolean isQueryChild = memberGroupService.isQueryChild(memberId);
        memberMerchantDetailQuery.setIsQueryChild(isQueryChild ? 1 : 0);
        return memberMerchantDetailService.queryList(memberMerchantDetailQuery);
    }

    /**
     * 统计数列表
     * @param memberId 会员编号
     * @param unit 单位（0:7天 1:6周 2:6个月）
     * @return 数据列表
     */
    public List<MemberMerchantDetail> analyzeList(String memberId, Integer unit){
        if (unit.equals(1) || unit.equals(2) || unit.equals(3)){
            return this.analyzeForUnitList(memberId, unit);
        }
        return this.analyzeList(memberId);
    }

    /**
     * 根据单位查询统计视图
     * @param memberId 会员编号
     * @param unit 单位（0:7天 1:6周 2:6个月 3:12个月）
     * @return 数据列表
     */
    public List<MemberMerchantDetail> analyzeForUnitList(String memberId, Integer unit){
        String startDay;
        String endDay;
        List<MemberMerchantDetail> list = Lists.newArrayList();
        int number = 0;
        if(unit.equals(3)){
            number = 11;
        }else if(unit.equals(1) ||unit.equals(2)){
            number = 5;
        }
        for (int i= number; i>=0; i--){
            startDay = 1 == unit? DateUtils.formatDate(DateUtils.getFirstDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i));
            endDay = 1 == unit? DateUtils.formatDate(DateUtils.getLastDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getLastDayOfMonth(-i));
            MemberMerchantDetailQuery query = new MemberMerchantDetailQuery();
            query.setCreateStart(startDay);
            query.setCreateEnd(endDay);
            query.setMemberId(memberId);
            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.sum(query);
            list.add(memberMerchantDetail);
        }
        return list;
    }

    /**
     * 统计所有
     * @return 统计数据
     */
    public List<MemberMerchantDetail> analyzeAdminList(){
        String now = DateUtils.todayDate();
        String startDay = DateUtils.formatDate(DateUtils.addDay(now, -7));
        MemberMerchantDetailQuery memberMerchantDetailQuery = new MemberMerchantDetailQuery();
        memberMerchantDetailQuery.setCreateStart(startDay);
        memberMerchantDetailQuery.setCreateEnd(now);
        return memberMerchantDetailService.allQueryList(memberMerchantDetailQuery);
    }

    /**
     * 根据单位统计所有
     * @param unit 单位（0:7天 1:6周 2:6个月 3:12个月）
     * @return 数据列表
     */
    public List<MemberMerchantDetail> analyzeAdminList(Integer unit){
        if (unit.equals(1) || unit.equals(2) || unit.equals(3)){
            return this.analyzeForUnitAdminList(unit);
        }
        return this.analyzeAdminList();
    }

    /**
     * 根据单位统计所有
     * @param unit
     * @return
     */
    public List<MemberMerchantDetail> analyzeForUnitAdminList(Integer unit){
        String startDay;
        String endDay;
        List<MemberMerchantDetail> list = Lists.newArrayList();
        int number = 0;
        if(unit.equals(3)){
            number = 11;
        }else if(unit.equals(1) ||unit.equals(2)){
            number = 5;
        }
        for (int i= number; i>=0; i--){
            startDay = 1 == unit? DateUtils.formatDate(DateUtils.getFirstDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i));
            endDay = 1 == unit? DateUtils.formatDate(DateUtils.getLastDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getLastDayOfMonth(-i));
            MemberMerchantDetailQuery query = new MemberMerchantDetailQuery();
            query.setCreateStart(startDay);
            query.setCreateEnd(endDay);
            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.allSum(query);
            list.add(memberMerchantDetail);
        }
        return list;
    }

    /**
     * 统计一段时间的数据
     * @param dayList 日期列表
     * @param merchantAnalyzeList 统计数据
     * @return 统计数据列表
     */
    public List<String> getCount(List<Date> dayList, List<MemberMerchantDetail> merchantAnalyzeList, Integer type){
        List<String> resultList = Lists.newArrayList();
        for (Date day : dayList){
            String date = DateUtils.formatDate(day);
            Object num = 0;
            for (MemberMerchantDetail memberMerchantDetail : merchantAnalyzeList){
                if (date.equals(DateUtils.formatDate(memberMerchantDetail.getCreateDate()))){
                    if (type == 0){
                        num = memberMerchantDetail.getApplyNum();
                    }
                    else if (type == 1){
                        num = memberMerchantDetail.getOrderNum();
                    }
                    else if (type == 2){
                        num = memberMerchantDetail.getMoney();
                        num = BigDecimalUtils.round((Double) num, 2);
                    }
                    else if (type == 3){
                        num = memberMerchantDetail.getViewNum();
                    }
                    else if (type == 4){
                        num = memberMerchantDetail.getMemberNum();
                    }
                    // 成功数
                    else if (type == 5) {
                        num = memberMerchantDetail.getSuccessNum();
                    }
                    else {
                        num = memberMerchantDetail.getApplyNum();
                    }
                    break;
                }
            }
            resultList.add(num.toString());
        }
        return resultList;
    }


    /**
     * 报名列表
     * @return 报名列表
     */
    public List<String> applyList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 0);
    }

    /**
     * 成功列表
     * @return 成功列表
     */
    public List<String> successList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 5);
    }

    /**
     * 报名列表
     * @return 报名列表
     */
    public List<String> applyForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){
        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getApplyNum());
            }
        });
    }

    /**
     * 成功列表
     * @return 成功列表
     */
    public List<String> successForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){
        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getSuccessNum());
            }
        });
    }

    /**
     * 订单列表
     * @return 列表数据
     */
    public List<String> orderList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 1);
    }

    /**
     * 订单列表
     * @return 列表数据
     */
    public List<String> orderForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){

        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getOrderNum());
            }
        });
    }


    /**
     * 资金列表
     * @return 列表数据
     */
    public List<String> paymentList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 2);
    }

    /**
     * 资金列表
     * @return 列表数据
     */
    public List<String> paymentForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){

        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getMoney());
            }
        });
    }

    /**
     * 浏览量列表
     * @return 数据列表
     */
    public List<String> viewList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 3);
    }

    /**
     * 浏览量列表
     * @return 数据列表
     */
    public List<String> viewForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){
        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getViewNum());
            }
        });
    }

    /**
     * 会员数据列表
     * @return 数据列表
     */
    public List<String> memberList(List<MemberMerchantDetail> merchantAnalyzeList){
        List<Date> dateList = this.dateList();
        return getCount(dateList, merchantAnalyzeList, 4);
    }

    /**
     * 会员数据列表
     * @return 数据列表
     */
    public List<String> memberForUnitList(List<MemberMerchantDetail> merchantAnalyzeList){
        return LangUtils.transform(merchantAnalyzeList, new Function<MemberMerchantDetail, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MemberMerchantDetail memberMerchantDetail) {
                return String.valueOf(memberMerchantDetail.getMemberNum());
            }
        });
    }

    /**
     * 合伙人数据统计
     *
     * @param memberId 会员编号
     * @return
     */
    public Map<String, CopartnerAnalyzeListOutput> copartnerList(String memberId) {
        HashMap<String, CopartnerAnalyzeListOutput> map = Maps.newHashMap();

        // 今天
        String now = DateUtils.todayDate();
        CopartnerAnalyzeQuery query = new CopartnerAnalyzeQuery();
        query.setCreateStart(now);
        query.setCreateEnd(now);
        query.setMemberId(memberId);

        MemberCopartnerDetail today = memberCopartnerDetailService.sum(query);
        CopartnerAnalyzeListOutput todayData = new CopartnerAnalyzeListOutput
                (today.getApplyNum(), today.getOrderNum(), today.getTotalMoney());
        map.put("today", todayData);

        // 昨天
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        query.setCreateStart(day);
        query.setCreateEnd(day);
        MemberCopartnerDetail yesterday = memberCopartnerDetailService.sum(query);
        CopartnerAnalyzeListOutput yesterdayData = new CopartnerAnalyzeListOutput
                (yesterday.getApplyNum(), yesterday.getOrderNum(), yesterday.getTotalMoney());
        map.put("yesterday", yesterdayData);

        // 本周
        query.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        query.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MemberCopartnerDetail week = memberCopartnerDetailService.sum(query);
        CopartnerAnalyzeListOutput weekData = new CopartnerAnalyzeListOutput
                (week.getApplyNum(), week.getOrderNum(), week.getTotalMoney());
        map.put("week", weekData);

        // 本月
        query.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        query.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MemberCopartnerDetail month = memberCopartnerDetailService.sum(query);
        CopartnerAnalyzeListOutput monthData = new CopartnerAnalyzeListOutput
                (month.getApplyNum(), month.getOrderNum(), month.getTotalMoney());
        map.put("month", monthData);

        //历史
        MemberCopartnerCount history = memberCopartnerCountService.findByMemberId(memberId);
        if (null == history) {
            history = new MemberCopartnerCount(0, 0, 0, 0d, 0d, 0d, 0d, 0d);
        }
        CopartnerAnalyzeListOutput historyData = new CopartnerAnalyzeListOutput
                (history.getApplyNum(), history.getOrderNum(), history.getTotalMoney());
        map.put("history", historyData);
        return map;
    }

    /**
     * 实时更新合伙人报名统计数
     *
     * @param activityId 活动编号
     */
    public void updateCopartnerAnalyzeApplyNum(String activityId) {
        TargetCopartner targetCopartner = targetCopartnerService.findByTargetId(activityId);
        if (null != targetCopartner) {
            /**更新合伙人报名数**/
            CopartnerAnalyze copartnerAnalyze = copartnerAnalyzeService.getSet(targetCopartner.getPartnerId(), DateUtils.today());
            if (null != copartnerAnalyze.getApplyNum()) {
                copartnerAnalyze.setApplyNum(copartnerAnalyze.getApplyNum() + 1);
            } else {
                copartnerAnalyze.setApplyNum(1);
            }
            copartnerAnalyzeService.update(copartnerAnalyze);
        }
    }

    /**
     * 实时更新合作商报名统计数
     *
     * @param memberId
     */
    public void updateMerchantAnalyzeApplyNum(String memberId) {
        if (StringUtils.isNotEmpty(memberId)) {
            /**更新合作商报名数**/
            MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(memberId, DateUtils.today());
            if (null != merchantAnalyze.getApplyNum()) {
                merchantAnalyze.setApplyNum(merchantAnalyze.getApplyNum() + 1);
            } else {
                merchantAnalyze.setApplyNum(1);
            }
            merchantAnalyzeService.update(merchantAnalyze);
        }
    }

    /**
     * 更新合伙人订单统计数
     *
     * @param activityId 活动编号
     * @param payment    订单金额
     */
    public void updateCopartnerAnalyzeOrder(String activityId, Float payment) {
        TargetCopartner targetCopartner = targetCopartnerService.findByTargetId(activityId);
        if (null != targetCopartner) {
            if (null != targetCopartner.getOrderNum()) {
                targetCopartner.setOrderNum(targetCopartner.getOrderNum() + 1);
            } else {
                targetCopartner.setOrderNum(1);
            }
            if (null != targetCopartner.getTotalMoney()) {
                double add = BigDecimalUtils.add(targetCopartner.getTotalMoney(), payment);
                double round = BigDecimalUtils.round(add, 2);
                targetCopartner.setTotalMoney(round);
            } else {
                targetCopartner.setTotalMoney(Double.valueOf(payment));
            }
            targetCopartnerService.update(targetCopartner);

            CopartnerAnalyze analyze = copartnerAnalyzeService.getSet(targetCopartner.getPartnerId(), DateUtils.today());
            if (null != analyze.getOrderNum()) {
                analyze.setOrderNum(analyze.getOrderNum() + 1);
            } else {
                analyze.setOrderNum(1);
            }
            if (null != analyze.getTotalMoney()) {
                double add = BigDecimalUtils.add(analyze.getTotalMoney(), payment);
                double round = BigDecimalUtils.round(add, 2);
                analyze.setTotalMoney(round);
            } else {
                analyze.setTotalMoney(Double.valueOf(payment));
            }
            copartnerAnalyzeService.update(analyze);
        }
    }

    /**
     * 更新合作商订单统计数
     *
     * @param memberId 合作商编号
     * @param payment  订单金额
     */
    public void updateMerchantAnalyzeOrder(String memberId, Float payment) {
        if (StringUtils.isNotEmpty(memberId)) {
            /**更新合作商报名数**/
            MerchantAnalyze analyze = merchantAnalyzeService.getSet(memberId, DateUtils.today());
            if (null != analyze.getOrderNum()) {
                analyze.setOrderNum(analyze.getOrderNum() + 1);
            } else {
                analyze.setOrderNum(1);
            }
            if (null != analyze.getMoney()) {
                double add = BigDecimalUtils.add(analyze.getMoney(), payment);
                double round = BigDecimalUtils.round(add, 2);
                analyze.setMoney(round);
            } else {
                analyze.setMoney(Double.valueOf(payment));
            }
            merchantAnalyzeService.update(analyze);
        }
    }


    public List<MerchantAnalyzeListOutput> transformDeatil(List<MemberMerchantDetailWithMember> list){
        List<MerchantAnalyzeListOutput> outputList
                = LangUtils.transform(list, new Function<MemberMerchantDetailWithMember, MerchantAnalyzeListOutput>() {
            @Nullable
            @Override
            public MerchantAnalyzeListOutput apply(@Nullable MemberMerchantDetailWithMember memberMerchantDetailWithMember) {
                return MerchantAnalyzeListOutput.transform(memberMerchantDetailWithMember);
            }
        });
        return outputList;
    }

    public List<MerchantAnalyzeListOutput> transformCount(List<MemberMerchantCountWithMember> list){
        List<MerchantAnalyzeListOutput> outputList
                = LangUtils.transform(list, new Function<MemberMerchantCountWithMember, MerchantAnalyzeListOutput>() {
            @Nullable
            @Override
            public MerchantAnalyzeListOutput apply(@Nullable MemberMerchantCountWithMember memberMerchantCountWithMember) {
                return MerchantAnalyzeListOutput.transform(memberMerchantCountWithMember);
            }
        });
        return outputList;
    }
    /**
     * 机构数据分析
     * @param memberMerchantDetailQuery 查询参数
     * @param page 分页参数
     * @return 分析列表
     */
    public List<MerchantAnalyzeListOutput> allList(MemberMerchantDetailQuery memberMerchantDetailQuery, Page page, String currentId){
        List<Map<String, String>> partners = memberPartnerPrivilegeService.findByMemberId(currentId);
        MemberAdministrator byMemberId = memberAdministratorService.getByMemberId(currentId);
        DataParam dataParam = new DataParam();
        if (partners.size() > 0) {
            dataParam.setShowAll(false);
            dataParam.setInitiatorId(currentId);
            dataParam.setJoinColumn("m.id");
        } else if(byMemberId != null){
            dataParam.setShowAll(true);
        }else {
            dataParam.setShowAll(false);
        }

        if (null != memberMerchantDetailQuery.getTimeType() && !memberMerchantDetailQuery.getTimeType().equals(0)
                && !memberMerchantDetailQuery.getTimeType().equals(5)){
            String now = DateUtils.todayDate();
            String endTime = DateUtils.todayDate();
            String startTime = null;
            if (memberMerchantDetailQuery.getTimeType().equals(1)){
                startTime = now;
            }
            else if (memberMerchantDetailQuery.getTimeType().equals(2)){
                startTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
                endTime = startTime;
            }
            else if (memberMerchantDetailQuery.getTimeType().equals(3)){
                startTime = DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0));
                endTime = DateUtils.formatDate(DateUtils.getLastDayOfWeek(0));
            }
            else if (memberMerchantDetailQuery.getTimeType().equals(4)){
                startTime = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0));
                endTime = DateUtils.formatDate(DateUtils.getLastDayOfMonth(0));
            }
            memberMerchantDetailQuery.setCreateStart(startTime);
            memberMerchantDetailQuery.setCreateEnd(endTime);
            List<MemberMerchantDetailWithMember> memberMerchantDetailWithMemberList
                    = memberMerchantDetailService.sumList(memberMerchantDetailQuery, dataParam, page);
            return this.transformDeatil(memberMerchantDetailWithMemberList);
        }
        List<MemberMerchantCountWithMember> memberMerchantCountWithMemberList
                = memberMerchantCountService.queryList(MemberMerchantCountQuery.transform(memberMerchantDetailQuery), dataParam, page);
        return this.transformCount(memberMerchantCountWithMemberList);
    }


    /**
     * admin统计详情
     * @return 详情
     */
    public Map<String, MainViewAnalyze> adminDetail(){
        Map<String, MainViewAnalyze> map = new HashMap<>();
        map.put("today", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"today","SYSTEM")));
        map.put("yesterday", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"yesterday","SYSTEM")));
        map.put("beforeYDay", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"beforeYDay","SYSTEM")));
        map.put("week", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"week","SYSTEM")));
        map.put("month", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"month","SYSTEM")));
        map.put("history", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"history","SYSTEM")));
        return map;
    }

    /**
     * 订单总额
     * @param params
     * @param orderForm
     * @return
     */
    public Double getOrderTotal(OrderForm orderForm, Map<String, Object> params, boolean isTxz, boolean isSuccess){
        Map<String, Object> newParams = Maps.newHashMap();
        if (isTxz) {
            Set<String> merchants = MerchantUtil.getTxzMerchants();
            newParams.put("txzMerchantId", merchants);
        }
        newParams.putAll(params);
        newParams.put("isSuccess", isSuccess);
        Double totalPayment = orderFormService.getTotalPayment(orderForm, newParams);
        return totalPayment == null ? 0d : totalPayment;
    }



    public HashMap<String, List> getOutPutMap(List<MemberMerchantDetail> list, Integer unit){
        HashMap<String, List> map = Maps.newHashMap();
        List<String> applyList;
        List<String> successList;
        List<String> orderList;
        List<String> paymentList;
        List<String> viewList;
        List<String> memberList;

        if (unit.equals(1) || unit.equals(2) || unit.equals(3)){
            applyList = this.applyForUnitList(list);
            successList = this.successForUnitList(list);
            orderList = this.orderForUnitList(list);
            paymentList = this.paymentForUnitList(list);
            viewList = this.viewForUnitList(list);
            memberList = this.memberForUnitList(list);
        }else {
            applyList = this.applyList(list);
            successList = this.successList(list);
            orderList = this.orderList(list);
            paymentList = this.paymentList(list);
            viewList = this.viewList(list);
            memberList = this.memberList(list);
        }
        map.put("applyList", applyList);
        map.put("successList", successList);
        map.put("orderList", orderList);
        map.put("paymentList", paymentList);
        map.put("viewList", viewList);
        map.put("memberList", memberList);
        return map;
    }
}
